package com.zxkj.support;

/**
 * @Author 沈颖峰
 * @Date 2019/6/5 19:44
 * @Description 统一API响应结果封装
 */
public class Result<T> {
    //("响应码")
    private int code = ResultCode.SUCCESS.code;

    //("响应信息")
    private String message = "";

    //("响应数据")
    private T data;

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    public Result(ResultCode resultCode, String message) {
        this.code = resultCode.code;
        this.message = message;
    }

    public Result(ResultCode resultCode, String message, T data) {
        this.code = resultCode.code;
        this.message = message;
        this.data = data;
    }

    public static Result success() {
        return new Result(ResultCode.SUCCESS, "success");
    }

    public static <T> Result success(T data) {
        return success().setData(data);
    }

    public static Result faild() {
        return new Result(ResultCode.FAIL, "faild");
    }

    public static Result faild(String msg) {
        return new Result(ResultCode.FAIL, msg);
    }

    public static <T> Result faild(String msg, T data) {
        return new Result(ResultCode.FAIL, msg, data);
    }

    public static <T> Result build(ResultCode code, String msg, T data) {
        return new Result(code, msg, data);
    }

    public Result setCode(ResultCode resultCode) {
        this.code = resultCode.code;
        return this;
    }

    public int getCode() {
        return code;
    }

    public Result setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return ("code : "
                + getCode()
                + ",message : "
                + getMessage()
                + ",data : "
                + getData().toString());
    }
}
